from django.shortcuts import render

# Create your views here.
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView
from drf_haystack.viewsets import HaystackViewSet

from goods.models import SKU
from mall.utils.pagination import StandardResultsSetPagination
from .serializers import SKUSerializer, SKUIndexSerializer


class SKUListView(ListAPIView):
    """SKU列表数据"""
    serializer_class = SKUSerializer
    # 排序
    filter_backends = [OrderingFilter]
    ordering_fields = ('create_time', 'price', 'sales')
    # 分页
    # pagination_class = StandardResultsSetPagination  # 全局配置

    def get_queryset(self):
        category_id = self.kwargs["category_id"]
        return SKU.objects.filter(category_id=category_id, is_launched=True)


class SKUSearchViewSet(HaystackViewSet):
    """SKU搜索"""
    index_models = [SKU]
    serializer_class = SKUIndexSerializer


